package com.peng.dao.impl;

import com.peng.dao.userDao;
import com.peng.entity.user;
import com.peng.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 欧阳欣鹏 on 2018/8/6.
 */
public class userDaoImpl implements userDao{
    @Override
    public List<user> li(int id) {
        List<user> li=new ArrayList<user>();
        Connection con=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        String sql="select * from user where uid=?";
        try {
            con= BaseDao.getConnection();
            st = con.prepareStatement(sql);
            st.setInt(1,id);
            rs = st.executeQuery();
            while (rs.next()) {
               user u=new user();
               u.setUid(rs.getInt("uid"));
               u.setUname(rs.getString("uname"));
               u.setUphone(rs.getString("uphone"));
               u.setUpassword(rs.getString("upassword"));
                u.setUpassword(rs.getString("upassword"));
                u.setUprovince(rs.getInt("uprovince"));
                u.setUcity(rs.getInt("ucity"));
                u.setUcounty(rs.getInt("ucounty"));
                u.setUaddress(rs.getString("uaddress"));
               u.setUsex(rs.getString("usex"));
               u.setUtype(rs.getInt("utype"));
               u.setCreateTime(rs.getString("createTime"));
               u.setOpenid(rs.getString("openid"));
               u.setCheck(rs.getInt("check"));
               u.setUimage(rs.getString("uimage"));
               u.setUemail(rs.getString("uemail"));
               li.add(u);
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }finally{
            BaseDao.closeConnection(con, st, rs);
        }
        return li;
    }

    @Override
    public int insert(user u) {
        String sql="insert into user value(null,?,?,?,?,?,?,?,?,?,NOW(),?,?,?,?)";
        Object[] ob={u.getUname(),u.getUphone(),u.getUpassword(),u.getUprovince(),u.getUcity(), u.getUcounty(), u.getUaddress(), u.getUsex(), u.getUtype(), u.getOpenid(), u.getCheck(), u.getUimage(), u.getUemail()
        };
        return BaseDao.idu(sql,ob);
    }

    @Override
    public int update(user s) {
        String sql="update `user` set uname=?,uphone=?,upassword=?,uprovince=?,ucity=?,ucounty=?,uaddress=?,utype=?,createTime=NOW(),openid=?,`check`=?,uimage=?,uemail=? where uid=?";
        Object[] ob={s.getUname(),s.getUphone(),s.getUpassword(),s.getUprovince(),s.getUcity(),s.getUcounty(),s.getUaddress(),s.getUtype(),s.getOpenid(),s.getCheck(),s.getUimage(),s.getUemail(),s.getUid()};
        return BaseDao.idu(sql,ob);
    }

    @Override
    public user findbyUid(int id) {
        user u=new user();
        String sql="select * from user where uid=?";
        Connection con=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            con= BaseDao.getConnection();
            st=con.prepareStatement(sql);
            st.setInt(1,id);
            rs=st.executeQuery();
            if (rs.next()) {
                u.setUid(rs.getInt("uid"));
                u.setUname(rs.getString("uname"));
                u.setUphone(rs.getString("uphone"));
                u.setUpassword(rs.getString("upassword"));
                u.setUprovince(rs.getInt("uprovince"));
                u.setUcity(rs.getInt("ucity"));
                u.setUcounty(rs.getInt("ucounty"));
                u.setUaddress(rs.getString("uaddress"));
                u.setUsex(rs.getString("usex"));
                u.setUtype(rs.getInt("utype"));
                u.setCreateTime(rs.getString("createTime"));
                u.setOpenid(rs.getString("openid"));
                u.setCheck(rs.getInt("check"));
                u.setUimage(rs.getString("uimage"));
                u.setUemail(rs.getString("uemail"));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            BaseDao.closeConnection(con, st, rs);
        }
        return u;

    }
}
